package com.jwt.furn.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwt.furn.bean.Furn;
import com.jwt.furn.service.FurnService;
import com.jwt.furn.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 前后端分离项目,后端返回json数据
 * 为了方便，直接在类上添加 @RestController复合注解,包含 @Controller+@ResponseBody
 * 这样每个方法上也就拥有了 @ResponseBody
 *
 * @author jiawentao
 * @date 2023-02-19
 */
@RestController
@Slf4j
@RequestMapping("/furn")
public class FurnController {

    @Resource
    private FurnService furnService;

    //1.前端如果是以json格式来发送添加信息furn,需要加 @RequestBody 注解
    //作用：将客户端提交的json数据，封装成javaBean对象
    //2.前端如果是以表单形式提交数据,则不需要使用 @RequestBody注解
    @PostMapping("/save")
    public Result save(@RequestBody Furn furn) {
//    public Result save(Furn furn){
        log.info("furn={}", furn);
        furnService.save(furn);
        return Result.success();//返回成功,不需要携带数据
    }

    //查询所有家居信息
    @RequestMapping("/list")
    public Result listFurns() {
        List<Furn> list = furnService.list();
        return Result.success(list);
    }

    //修改家居信息
    @PutMapping("/update")
    public Result update(@RequestBody Furn furn) {
        furnService.updateById(furn);
        return Result.success();
    }

    //删除家居信息
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        furnService.removeById(id);
        return Result.success();
    }

    //根据id查家居信息
    @GetMapping("/find/{id}")
    public Result getFurnById(@PathVariable("id") Integer id) {
        Furn furn = furnService.getById(id);
        return Result.success(furn);
    }

    //分页查询的接口
    @RequestMapping("/furnsByPage")
    public Result listFurnsByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "5") Integer pageSize) {
        //这里通过page方法，返回page对象，里面封装的分页数据
        Page<Furn> page = furnService.page(new Page<>(pageNum, pageSize));
        return Result.success(page);
    }


    //根据条件检索
    @RequestMapping("/furnsByConditionPage")
    public Result furnsByConditionPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "5") Integer pageSize,
                                       @RequestParam(defaultValue = "") String search) {
        QueryWrapper<Furn> queryWrapper = Wrappers.query();
        if (StringUtils.hasText(search)){
            //like 后第一个参数是表字段
            queryWrapper.like("name",search);
        }

        Page<Furn> page = furnService.page(new Page<>(pageNum, pageSize),queryWrapper);
        return Result.success(page);
    }

    //根据条件检索2
    @RequestMapping("/furnsByConditionPage2")
    public Result furnsByConditionPage2(@RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "5") Integer pageSize,
                                       @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Furn> lambdaQueryWrapper = Wrappers.lambdaQuery();

        if (StringUtils.hasText(search)){
            //like 后第一个参数是表字段
            lambdaQueryWrapper.like(Furn::getName,search);
        }

        Page<Furn> page = furnService.page(new Page<>(pageNum, pageSize),lambdaQueryWrapper);
        log.info("page={}",page.getRecords());
        return Result.success(page);
    }

}
